#include "mainwindow.h"
#include <QMenuBar>
#include <QToolBar>
#include <QStatusBar>
#include <QAction>
#include <QMessageBox>
#include <QApplication>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    // 设置窗口标题和大小
    setWindowTitle(tr("Qt5 示例程序"));
    resize(800, 600);

    // 初始化各个组件
    createMenus();
    createToolBars();
    createStatusBar();
    setupCentralWidget();

    // 显示状态信息
    statusBar()->showMessage(tr("欢迎使用 Qt5 示例程序"));
}

MainWindow::~MainWindow()
{
}

void MainWindow::createMenus()
{
    // 创建文件菜单
    m_fileMenu = menuBar()->addMenu(tr("文件(&F)"));

    m_newAct = new QAction(tr("新建(&N)"), this);
    m_newAct->setShortcut(tr("Ctrl+N"));
    m_newAct->setStatusTip(tr("创建一个新文件"));
    connect(m_newAct, &QAction::triggered, this, &MainWindow::onNewFile);

    m_openAct = new QAction(tr("打开(&O)"), this);
    m_openAct->setShortcut(tr("Ctrl+O"));
    m_openAct->setStatusTip(tr("打开一个已有的文件"));
    connect(m_openAct, &QAction::triggered, this, &MainWindow::onOpenFile);

    m_saveAct = new QAction(tr("保存(&S)"), this);
    m_saveAct->setShortcut(tr("Ctrl+S"));
    m_saveAct->setStatusTip(tr("保存当前文件"));
    connect(m_saveAct, &QAction::triggered, this, &MainWindow::onSaveFile);

    m_fileMenu->addSeparator();

    m_exitAct = new QAction(tr("退出(&X)"), this);
    m_exitAct->setShortcut(tr("Ctrl+Q"));
    m_exitAct->setStatusTip(tr("退出应用程序"));
    connect(m_exitAct, &QAction::triggered, qApp, &QApplication::quit);

    m_fileMenu->addAction(m_newAct);
    m_fileMenu->addAction(m_openAct);
    m_fileMenu->addAction(m_saveAct);
    m_fileMenu->addSeparator();
    m_fileMenu->addAction(m_exitAct);

    // 创建编辑菜单
    m_editMenu = menuBar()->addMenu(tr("编辑(&E)"));

    m_cutAct = new QAction(tr("剪切(&T)"), this);
    m_cutAct->setShortcut(tr("Ctrl+X"));
    m_cutAct->setStatusTip(tr("剪切选中内容"));
    m_editMenu->addAction(m_cutAct);

    m_copyAct = new QAction(tr("复制(&C)"), this);
    m_copyAct->setShortcut(tr("Ctrl+C"));
    m_copyAct->setStatusTip(tr("复制选中内容"));
    m_editMenu->addAction(m_copyAct);

    m_pasteAct = new QAction(tr("粘贴(&P)"), this);
    m_pasteAct->setShortcut(tr("Ctrl+V"));
    m_pasteAct->setStatusTip(tr("粘贴内容"));
    m_editMenu->addAction(m_pasteAct);

    // 创建帮助菜单
    m_helpMenu = menuBar()->addMenu(tr("帮助(&H)"));

    m_aboutAct = new QAction(tr("关于(&A)"), this);
    m_aboutAct->setStatusTip(tr("显示关于对话框"));
    connect(m_aboutAct, &QAction::triggered, this, &MainWindow::onAbout);

    m_helpMenu->addAction(m_aboutAct);
}

void MainWindow::createToolBars()
{
    // 创建文件工具栏
    m_fileToolBar = addToolBar(tr("文件"));
    m_fileToolBar->addAction(m_newAct);
    m_fileToolBar->addAction(m_openAct);
    m_fileToolBar->addAction(m_saveAct);

    // 创建编辑工具栏
    m_editToolBar = addToolBar(tr("编辑"));
    m_editToolBar->addAction(m_cutAct);
    m_editToolBar->addAction(m_copyAct);
    m_editToolBar->addAction(m_pasteAct);
}

void MainWindow::createStatusBar()
{
    m_statusBar = statusBar();
}

void MainWindow::setupCentralWidget()
{
    // 创建中心部件
    m_centralWidget = new MyWidget(this);
    setCentralWidget(m_centralWidget);
}

void MainWindow::onNewFile()
{
    QMessageBox::information(this, tr("新建文件"), tr("新建文件功能被调用"));
}

void MainWindow::onOpenFile()
{
    QMessageBox::information(this, tr("打开文件"), tr("打开文件功能被调用"));
}

void MainWindow::onSaveFile()
{
    QMessageBox::information(this, tr("保存文件"), tr("保存文件功能被调用"));
}

void MainWindow::onAbout()
{
    QMessageBox::about(this, tr("关于"), 
                       tr("Qt5 示例程序\n" 
                          "版本 1.0.0\n" 
                          "使用 C++ 和 Qt5 开发的示例应用程序"));
}